using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;

namespace Beetle;

public class DataWriter : IDataWriter
{
	private bool a = true;

	public Encoding Coding = Encoding.UTF8;

	private Stream b;

	private byte[] c = new byte[8];

	[CompilerGenerated]
	private IChannel d;

	public bool LittleEndian
	{
		get
		{
			return a;
		}
		set
		{
			a = value;
		}
	}

	public long Position
	{
		get
		{
			return b.Position;
		}
		set
		{
			b.Position = value;
		}
	}

	public long Length => b.Length;

	public IChannel Channel
	{
		[CompilerGenerated]
		get
		{
			return d;
		}
		[CompilerGenerated]
		set
		{
			d = value;
		}
	}

	public DataWriter(Stream stream, bool littleEndian)
	{
		b = stream;
		a = littleEndian;
	}

	public void Reset()
	{
	}

	public void SetStream(Stream stream)
	{
		b = stream;
	}

	public void Write(bool value)
	{
		if (value)
		{
			Write((byte)1);
		}
		else
		{
			Write((byte)0);
		}
	}

	public void Write(byte value)
	{
		b.WriteByte(value);
	}

	public unsafe void Write(short value)
	{
		if (!a)
		{
			value = j.a(value);
		}
		fixed (byte* ptr = &c[0])
		{
			*(short*)ptr = value;
		}
		b.Write(c, 0, 2);
	}

	public unsafe void Write(int value)
	{
		if (!a)
		{
			value = j.a(value);
		}
		fixed (byte* ptr = &c[0])
		{
			*(int*)ptr = value;
		}
		b.Write(c, 0, 4);
	}

	public unsafe void Write(long value)
	{
		if (!a)
		{
			value = j.a(value);
		}
		fixed (byte* ptr = &c[0])
		{
			*(long*)ptr = value;
		}
		b.Write(c, 0, 8);
	}

	public unsafe void Write(ushort value)
	{
		if (!a)
		{
			value = j.a(value);
		}
		fixed (byte* ptr = &c[0])
		{
			*(ushort*)ptr = value;
		}
		b.Write(c, 0, 2);
	}

	public unsafe void Write(uint value)
	{
		if (!a)
		{
			value = j.a(value);
		}
		fixed (byte* ptr = &c[0])
		{
			*(uint*)ptr = value;
		}
		b.Write(c, 0, 4);
	}

	public unsafe void Write(ulong value)
	{
		if (!a)
		{
			value = j.a(value);
		}
		fixed (byte* ptr = &c[0])
		{
			*(ulong*)ptr = value;
		}
		b.Write(c, 0, 8);
	}

	public void Write(DateTime value)
	{
		Write(value.Ticks);
	}

	public void Write(char value)
	{
		short value2 = (short)value;
		Write(value2);
	}

	public unsafe void Write(float value)
	{
		int value2 = *(int*)(&value);
		Write(value2);
	}

	public unsafe void Write(double value)
	{
		long value2 = *(long*)(&value);
		Write(value2);
	}

	public void Write(IMessage message)
	{
		message.Save(this);
	}

	public void Write<T>(IList<T> value) where T : IMessage
	{
		if (value != null && value.Count > 0)
		{
			Write(value.Count);
			for (int i = 0; i < value.Count; i++)
			{
				Write(value[i]);
			}
		}
		else
		{
			Write(0);
		}
	}

	public void WriteString(string value)
	{
		x.f().b(value, Coding, b);
	}

	public unsafe void WriteUTF(string value)
	{
		ushort num = 0;
		if (!string.IsNullOrEmpty(value))
		{
			num = (ushort)x.f().b(value, Coding);
		}
		ushort num2 = (a ? num : j.a(num));
		fixed (byte* ptr = &c[0])
		{
			*(ushort*)ptr = num2;
		}
		b.Write(c, 0, 2);
		b.Write(x.f().a, 0, num);
	}

	public unsafe void Write(string value)
	{
		int num = 0;
		if (!string.IsNullOrEmpty(value))
		{
			num = x.f().b(value, Coding);
		}
		if (!a)
		{
			j.a(num);
		}
		fixed (byte* ptr = &c[0])
		{
			*(int*)ptr = num;
		}
		b.Write(c, 0, 4);
		b.Write(x.f().a, 0, num);
	}

	public void Write(ByteArraySegment sagment)
	{
		Write(sagment.Count);
		b.Write(sagment.Array, 0, sagment.Count);
	}

	public void Write(byte[] buffer, int offset, int count)
	{
		b.Write(buffer, offset, count);
	}
}
